#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>


int main()
{
    struct stat file_stat;
    int ret = 0;

    /* 获取文件属性 */
    ret = stat("./test_file", &file_stat);
    if (-1 == ret) {
        perror("stat error");
        exit(-1);
    }

    /* 打印文件类型 */
    switch (file_stat.st_mode & __S_IFMT) {
        case __S_IFSOCK: printf("socket\n"); break;
        case __S_IFLNK: printf("symbolic link\n"); break;
        case __S_IFREG: printf("regular file\n"); break;
        case __S_IFBLK: printf("block device\n"); break;
        case __S_IFDIR: printf("directory\n"); break;
        case __S_IFCHR: printf("character device\n"); break;
        case __S_IFIFO: printf("FIFO\n"); break;
        default: perror("__S_IFMT error"); break;
    }

    /* 判断其他用户是否有可读权限 */
    if (file_stat.st_mode & S_IROTH) {
        printf("Read: YES\n");
    } else {
        printf("Read: NO\n");
    }

    /* 判断其他用户是否有可写权限 */
    if (file_stat.st_mode & S_IWOTH) {
        printf("Write: YES\n");
    } else {
        printf("Write: NO\n");
    }

    exit(0);
}